package com.eascs.p2p.admin.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.admin.manager.IBannerManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.BannerSetEntity;
import com.eascs.p2p.common.vo.BannerVO;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Repository
public class BannerManagerImpl extends BaseDao implements IBannerManager {

	@Override
	public PageResult<Object[]> findPageBannerSetEntity(BannerVO vo) {
	
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT id,code,name,url,banner_order,case when isDisplay=1 then '启用' else  '暂停' end as isDisplay from p2p_banner_set where 1=1");
		// 姓名
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and name like ?");
			
			sqlPageQuery.addParam("%" + vo.getName()+ "%");

		}
		if (StringUtil.isNotBlank(vo.getCode())) {
			sql.append(" and  code like ?");
			sqlPageQuery.addParam("%" + vo.getCode() + "%");
		}
		
		sql.append(" order by id desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

	@Override
	public String addBannerSetEntity(BannerSetEntity bannerSetEntity) {
	
		Date date = new Date();
		bannerSetEntity.setCreateTime(date);
		bannerSetEntity.setUpdateTime(date);
		bannerSetEntity.setIsDisplay(1);
		User user = UserHolder.getUser();
		if (user != null) {
			bannerSetEntity.setCreator(UserHolder.getUser().getCname());
			bannerSetEntity.setRegenerator(UserHolder.getUser().getCname());
		}
		boolean flag = this.insert(bannerSetEntity);
		if (flag == true) {
			return bannerSetEntity.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateBannerSetEntity(BannerSetEntity bannerSetEntity) {
		
		Date date = new Date();
		bannerSetEntity.setUpdateTime(date);
		User user = UserHolder.getUser();
		if (user != null) {
			bannerSetEntity.setRegenerator(UserHolder.getUser().getCname());

		}
		return this.updateById(bannerSetEntity);
	}

	@Override
	public BannerSetEntity findBannerSetEntityDetail(String id) {
	
		 return this.getById(BannerSetEntity.class, id);
	}

	@Override
	public boolean updateBannerStatus(String id, int isDisplay) {
		String sql = " update p2p_banner_set set isDisplay=? where id=?";
		return this.execute(sql, isDisplay,id);
	}

	@Override
	public List<BannerSetEntity> getBannerSetEntity(String code) {
		String sql="select * from p2p_banner_set where isDisplay=1 and code=? ";
		return this.findEntitys(sql, BannerSetEntity.class, code);
	}
	

	

}
